import { _decorator, Component, Node } from 'cc';
import { PalBattleEnemy, PalBattleEnemyOriginData, PalBattleHero, PalBattleHeroOriginData, PalBattleSkill, PalBattleSkillOriginData } from './PalBattleSystem';
import { enemy_001_草妖 } from '../data/enemy/001_草妖';
import { hero_001_李逍遥 } from '../data/hero/001_李逍遥';
import { hero_002_赵灵儿 } from '../data/hero/002_赵灵儿';
import { skill_001_气疗术 } from '../data/skill/skill_001_气疗术';
import { skill_002_五气朝元 } from '../data/skill/skill_002_五气朝元';
const { ccclass, property } = _decorator;

@ccclass('DataManager')
export class DataManager extends Component {
    public static instance = null;
    public static getInstance<DataManager>(): DataManager {
        if (!this.instance) this.instance = new this() as DataManager;
        return this.instance;
    }
    installSingletion() {
        if (!DataManager.instance) {
            DataManager.instance = this;
        }
    }
    protected onLoad(): void {
        this.installSingletion();
    }

    enemies: PalBattleEnemyOriginData[] = [];
    heros: PalBattleHeroOriginData[] = [];
    skills: PalBattleSkillOriginData[] = [];

    protected start(): void {
        this.loadEnemiesData();
        this.loadHerosData();
        this.loadSkillsData();
    }
    loadEnemiesData() {
        this.enemies.push(enemy_001_草妖);
    }
    loadHerosData() {
        this.heros.push(hero_001_李逍遥);
        this.heros.push(hero_002_赵灵儿);
    }
    loadSkillsData() {
        this.skills.push(skill_001_气疗术);
        this.skills.push(skill_002_五气朝元);
    }

    getEnemy(enemyID: number): PalBattleEnemy {
        const originData = this.enemies.find(enemy => enemy.enemyID === enemyID);
        return {
            originData: originData,
            saveData: {
                UUID: this.generatorUUID(),
                hp: originData.hp,
                武术: originData.武术,
                灵力: originData.灵力,
                防御: originData.防御,
                身法: originData.身法,
                吉运: originData.吉运,
                dead: false,
                defense: false,
                stun: false,
            }
        };
    }

    getHero(heroID: number): PalBattleHero {
        const originData = this.getHeroOriginData(heroID);
        return {
            originData: originData,
            saveData: {
                UUID: this.generatorUUID(),
                heroID: originData.heroID,
                hp: originData.hp,
                mp: originData.mp,
                灵力: originData.灵力,
                武术: originData.武术,
                防御: originData.防御,
                身法: originData.身法,
                吉运: originData.吉运,
                level: 1,
                exp: 0,
                maxExp: 15,
                dead: false,
                stun: 0,
                defense: false,
                skills: [],
            }
        };
    }
    getHeroOriginData(heroID: number): PalBattleHeroOriginData {
        return this.heros.find(hero => hero.heroID === heroID);
    }
    getSkillOriginData(skillID: number): PalBattleSkillOriginData {
        return this.skills.find(skill => skill.skillID === skillID);
    }
    getSkill(skillID: number): PalBattleSkill {
        const originData = this.getSkillOriginData(skillID);
        return {
            originData: originData,
            saveData: {
                UUID: this.generatorUUID(),
            }
        };
    }

    public generatorUUID() {
        return Math.random().toString(36).substring(2);
    }
}


